/**
 * Created by xu on 2015/12/19.
 */

$(document).ready(function(){
    getThresoldGroupList();
    getThresoldData();
});



//table相关======================================================================================
function getThresoldData(){
    var column=[
       // {title:checkBoxFormat(),name:"",width:"5%",align:"center",format:checkBoxFormat},
        {title:"阈值编号",name:"minorCode",width:"15%",align:"center",showTips:true},
        {title:"阈值名称",name:"shortName",width:"15%",align:"center"},
        {title:"最小值",name:"minVal",width:"15%",align:"center"},
        {title:"最大值",name:"maxVal",width:"15%",align:"center"},
        //{title:"状态",name:"",width:"15%",align:"center",format:statFormat},
        {title:"单位",name:"unit",width:"15%",align:"center"},
        {title:"编辑",name:"",width:"15%",align:"center",format:editFormat}
    ];

    $('#thresold').initTable({
        url:p_path+"/settings/threshold/queryThresholdList.json",
        columnData:column,
        selectDataNow:true,
        isBindDataToTr:true
        //afterLoadTable:afterLoadTableFun
    });
}

function afterLoadTableFun(){
    $("#thresold .on-off").click(function(event){
        $(this).toggleClass("active");
        event.stopPropagation();
    });

}

function checkBoxFormat(){
    var ck="<span class=\"table-ck\"><span class=\"check-box selected\" onclick=\"\"><i class=\"\"></i></span></span>";
    return ck;
}

function statFormat(json){
    var acti=json.state;
    var bar="<em onclick=updateState('"+json.id+"',"+acti+") class=\"on-off "+((acti==null||acti=='1')?"":"active")+" \"></em>";
    return bar;
}

function editFormat(){
    var ed="<a onclick='showTrdata(this)' href=\"#add\" data-toggle=\"modal\" class=\"insp-containeredit0\"></a>";
    return ed;
}

function showTrdata(a){
    var tr=$(a).parent().parent();
    var json=tr.data("trData");
    var showField=['shortName','majorCode','updateRemark','minorCode','unit','sort','marjorName'];
    for (var i in showField){
        $("#d-"+showField[i]).html(json[showField[i]]);
    }
    var stat=(json.state==null||json.state=='1')?"关闭":"开启";
    $("#d-state").html(stat);
    $("#d-id").val(json.id);
    $("#d-maxVal").val(json.maxVal);
    $("#d-minVal").val(json.minVal);
}

//查询相关======================================
function queryForm(){

    /*var ev = $(".table-ck .check-box i").get(0);
    var c=$._data(ev);
    console.log(c);
    return;*/

    var qData=$("#queryForm").serializeFormJSON();
    //var qData={"yzCode":$("#yzCode").val(),"yzName":$("#yzName").val()};
    $("#thresold").selectDataAfterSetParm(qData);
}
function getThresoldGroupList(){
    $().invoke(
        p_path+"/menu/queryThresholdGroupList.json",
        {},
        function(m,r){
            for(var i in r){
                $("#byGroupNameUL").append("<li ><a href=\"javascript:void(0);\" vid=\""+(r[i]["majorCode"])+"\" >"+(r[i]["shortName"])+"</a></li>")
            }
        }
    )
}



function updateState(id,em){
    var s=em==0?1:0;
    var d={"state":s,"id":id};
    if (id==null){
        alert("没有获取到id");
        return;
    }
    //todo 控制状态的，不需要 editData(d);
}

//修改相关
function editData(param){
    var eData={};
    if (param==null){
        var id=$("#d-id").val();
        var minV=$('#d-minVal').val();
        var maxV=$('#d-maxVal').val();
        var reMark=$('#d-updateRemark').val();
        if (id==null||minV==null||maxV==null){
            alert("请填写最大值和最小值");
            return;
        }
        eData={"id":id,"minVal":minV,"maxVal":maxV,"updateRemark":reMark};
    }else {
        eData=param;
    }

    $().invoke(
        p_path+"/settings/threshold/editThreshold.json",
        eData,
        function(m,r){
            if (param==null){
                alert(r);
                $('#add').modal('hide');
            }else {
                flashMessage_(r);
            }
            $('#thresold').refresh();
        }
    )
}